<!doctype html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<meta name="author" content="Adrian Toșcă">
		<meta name="description" content="Write QR Code to SVG with ZXing javascript library">
		<title>ZXing | Write QR Code to SVG</title>
		<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300italic,700,700italic">
		<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/3.0.3/normalize.css">
		<link rel="stylesheet" href="//cdn.rawgit.com/milligram/milligram/master/dist/milligram.min.css">
	</head>
	<body>

		<main class="wrapper">

			<section class="container" id="demo-content">
				<h1 class="title">Write QR Code to SVG</h1>
				
				<p>This example shows how to write a QR code to SVG with ZXing javascript library.

				<div>
					<label for="textInput">Content to write in QR code</label>
				</div>
				<div>
					<textarea id="textInput" maxlength="256">QR code (abbreviated from Quick Response Code) is the trademark for a type of matrix barcode (or two-dimensional barcode) first designed for the automotive industry in Japan.</textarea>
				</div>

				<div>
					<a class="button" id="writeButton">Write</a>
                    <a class="button" id="saveButton" disabled="disabled">Save File</a>
				</div>

				<label>Result:</label>
				<div id="result"></div>
				
				<p>See the <a href="https://github.com/aleris/zxing-typescript/tree/master/docs/examples/qr-svg-writer/">source code</a>  for this example.</p>

			</section>

			<footer class="footer">
				<section class="container">
					<p>ZXing TypeScript Demo. Licensed under the <a target="_blank" href="https://github.com/aleris/zxing-typescript#license" title="Apache License, Version 2.0">Apache License, Version 2.0</a>.</p>
				</section>
			</footer>

		</main>

		<script type="text/javascript" src="../zxing.qrcodewriter.min.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2014-11-29/FileSaver.js"></script>
        <script type="text/javascript">
			window.addEventListener('load', () => {
				const codeWriter = new ZXing.BrowserQRCodeSvgWriter('result')
				console.log('ZXing code writer initialized')

                let svgElement;
				document.getElementById('writeButton').addEventListener('click', () => {
                    const input = document.getElementById('textInput').value
					svgElement = codeWriter.write(input, 300, 300)
					console.log(`done writing`)
                    document.getElementById('saveButton').removeAttribute('disabled')
				})

                document.getElementById('saveButton').addEventListener('click', () => {
                    const svgData = (new XMLSerializer()).serializeToString(svgElement)
                    const blob = new Blob([svgData])
                    saveAs(blob, 'zxing-qrcode-example.svg')
				})

			})
		</script>

	</body>
</html>